package com.ham.mud.commands;

import com.ham.mud.ServerConnection;
import com.ham.mud.characters.player.Player;
import com.ham.mud.rooms.Exit;
import com.ham.mud.rooms.Room;
import com.ham.mud.rooms.RoomService;

/**
 * Created by hlucas on Jun 16, 2011 at 1:48:24 PM
 */
public class MoveCommand extends PlayerCommand {
    @Override
    protected String getCommandName() {
        return "move";
    }                   

    int x;
    int y;

    public MoveCommand(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public void execute(ServerConnection connection, String[] params) {
        if(params.length > 0) return; //todo: treat this as the move command

        Player player = connection.getPlayer();
        int atX = player.getAtX();
        int atY = player.getAtY();

        Room room = RoomService.getRoom(player.getZone(), atX, atY);
        Exit exit = room.getExit(x, y);
        
        if(exit == null) {
            connection.printSolo("Alas, you cannot move that way.");
            return;
        }

        if(exit.hasClosedDoor()) {
            connection.printSolo("Alas, there is a door in the way.");
            return;
        }

        int move = player.getVitals().getMove();
        if(move < 1) {
            connection.printSolo("You are too exhausted.");
            return;
        }

        player.getVitals().decrementMove(1);

        room.removePlayer(connection, RoomService.getExitString(x, y));

        String zone = exit.getZone();
        if(zone != null) {
            player.setZone(zone);
            player.setAtX(exit.getAtX());
            player.setAtY(exit.getAtY());
        } else {
            player.setAtX(atX + x);
            player.setAtY(atY + y);
        }

        String cameFrom = RoomService.getExitString(-x, -y);
        Room newRoom = RoomService.getRoom(player.getZone(), player.getAtX(), player.getAtY());
        newRoom.addPlayer(connection, cameFrom);

        new LookCommand().execute(connection, new String[0]);
    }
}
